package collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 对字符串集合进行排序
 */
public class SoretString {
    public static void main(String[] args) {
        /*
        List<String> list=new ArrayList<>();
        list.add("black");
        list.add("tom");
        list.add("Jack");
        list.add("lucy");
        list.add("laky");
        System.out.println("list的原始数据:"+list);//[Jack, black, tom, lucy, laky]
        //对英文字符串排序时候,会按首字母的ASCII码来排序
        //若首字母相同,则比较第2个字母的ASCII码,以此类推
        Collections.sort(list);
        System.out.println("list排序后:"+list);//[Jack, black, lucy, tom, laky]
         */

        List<String> list=new ArrayList<>();
        list.add("李汝骁");
        list.add("李瑞");
        list.add("马超明真帅");
        System.out.println("list原始数据"+list);
        /*
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //return o1.length()-o2.length();
                return o2.length()-o1.length();
            }
        });
         */
        Collections.sort(list,(o1,o2)->o1.length()-o2.length());

        System.out.println("list排序后数据"+list);
    }
}

